<?
require_once(_FRAMEWORK."/core/core.php");
/// @brief Define Core class 
/// @author ggugi
/// @date 2012-07-03
/// @params 
/// @return 
class core_controller extends core
{
	private static $instance;
    public static $sUserIdx;

	public static function &getInstance()
	{
		return self::$instance;
	}

	function __construct()
	{
		parent::__construct();
        if(!empty(parent::$oParam['SessionId'])){
            self::$sUserIdx = self::getUserIdx(parent::$oParam['SessionId']);
        }
		self::$instance =& $this;
	}
	
	/**
	 * @brief 공통 출력 메서드 
	 * @date 2012-07-03
	 **/
	protected function output( $sType, $oResult ) 
	{
		switch( strtolower($sType) ) {
			case "json":
				$result = json_encode($oResult);
			break;
		}	

        return $result;
	}

	/**
	 * @brief http 인자 가져오기 메서드
	 * @date 2012-07-03
	 **/
    protected function getParam($mKey = array())
    {
        $aKey = (is_string($mKey) || is_numeric($mKey)) ? array($mKey) : $mKey;
        $aTemp = array();
        foreach($aKey as $key => $val){
            $aTemp[$val] = parent::$oParam[$val];
        }

        return (is_string($mKey) || is_numeric($mKey)) ? parent::$oParam[$mKey] : $aTemp;
    }

	/**
	 * @brief 세션 추출 메서드 
	 * @date 2012-07-03
	 **/
    protected function getSession($mKey)
    {
        $aKey = (is_string($mKey) || is_numeric($mKey)) ? array($mKey) : $mKey;
        $aTemp = array();
        foreach($aKey as $key => $val){
            $aTemp[$val] = $_SESSION[$val];
        }
        
        return (is_string($mKey) || is_numeric($mKey)) ? $_SESSION[$mKey] : $aTemp;
    }

	/**
	 * @brief 세션 등록 메서드 
	 * @date 2012-07-03
	 **/
    protected function setSession($mKey)
    {
        if(is_string($mKey) || is_numeric($mKey)) return parent::doError('001');
        foreach($mKey as $key => $val){
            if(is_numeric($key)) return parent::doError('001');
            $_SESSION[$key] = $val;
        }
        
        return true;
    }

    /**
     * @brief 
     * @author hkkwon
     * @param                 |                         |        
     * @return                 |                         |        
     **/
     public function setActivity($_start_time, $_end_time) 
     {
        if(parent::$sGroup == "admin") return 0;
        $_end_time = explode(" ", microtime());
        $_during_time = ($_end_time[0] + $_end_time[1]) - ($_start_time[0] + $_start_time[1]);
        $oData = array(
            "Group" => parent::$sGroup,
            "Method" => parent::$sMethod,
            "SeqNo" => self::getParam('SeqNo'),
            "Param" => parent::$oParam,
            "Return" => parent::$oResult,
            "Result" => parent::$oResult['Result'],
            "ExecuteTime" => $_during_time,
            "UpdateAt"  => time(),
            "CreateAt"  => time()
        );
        parent::$model->set->setActivity($oData, self::$sUserIdx);
     }

    /**
     * @brief 
     * @author hkkwon
     * @param                 |                         |        
     * @return                 |                         |        
     **/
     public static function getActivity($sType="mongo") 
     {
        if(parent::$bIsCache == false) return null;
        if($sType == "mongo"){
            $oResult = parent::$model->get->getActivity(self::$sUserIdx);
            return $oResult['Return'];
        }elseif($sType == "memcached"){
            $sKey = implode(array(self::$sUserIdx, parent::$sGroup, parent::$sMethod));
            return self::getCache($sKey);
        }
     }

    /**
     * @brief 
     * @author hkkwon
     * @param                 |                         |        
     * @return                 |                         |        
     **/
     public static function getCache($sKey) 
     {
        $oMemcached = helper('memcached');
        return $oMemcached::get($sKey); 
     }

    /**
     * @brief 
     * @author hkkwon
     * @param                 |                         |        
     * @return                 |                         |        
     **/
     public static function setCache($aParams = array("key" => "", "data" => array(), "expire_time" => 0)) 
     {
        if(parent::$bIsCache == false) return null;
        $oMemcached = helper('memcached');
        return $oMemcached::set($aParams['key'], $aParams['data'], $aParams['expire_time']); 
     }

    /**
     * @brief 
     * @author hkkwon
     * @param                 |                         |        
     * @return                 |                         |        
     **/
     protected function getSessionId($sUserIdx, $sDeviceToken) 
     {
        return helper('cipher')->encrypt($sUserIdx."|".$sDeviceToken ,_KEY);
        
     }

    /**
     * @brief 
     * @author hkkwon
     * @param                 |                         |        
     * @return                 |                         |        
     **/
     protected function getUserIdx($sSessionId) 
     {
        //return $sSessionId;
        $sDecrypt = helper('cipher')->decrypt($sSessionId, _KEY);
        $aDecrypt = explode("|", $sDecrypt);
        return $aDecrypt[0];
     }
}

